package com.cloud.wms.ui.inside.transfer;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;

import com.cloud.core.eventBus.Event;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.TransferDownContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.presenter.TransferDownPresenter;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by FC on 2021/5/17.
 * Desc：移库下架扫描容器
 */
public class TransferDownContainerActivity extends BaseBarActivity<TransferDownPresenter> implements TransferDownContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    ArrayList<TransferCommodityEntity> entityList;

    @Override
    protected String barTitle() {
        return "移库下架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_transfer_scan_container;
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        entityList = getIntent().getParcelableArrayListExtra(IntentKey.LIST_KEY);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        mPresenter.checkContainer(result);
    }

    @Override
    public void onSuccess(Message message) {
        if (message.what == TransferDownPresenter.CHECK_BOX_SUCCESS) {
            DetachBoxBean boxBean = (DetachBoxBean) message.obj;
            if (entityList.size() == 1)
                TransferDownConfirmActivity.jumpToNext(this, entityList.get(0), boxBean);
            else
                TransChooseBatchActivity.jumpToNext(this, entityList, true, boxBean);
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                finish();
                break;
        }
    }

    public static void jumpToNext(Activity activity, ArrayList<TransferCommodityEntity> entityList) {
        Intent intent = new Intent(activity, TransferDownContainerActivity.class);
        intent.putParcelableArrayListExtra(IntentKey.LIST_KEY, entityList);
        activity.startActivity(intent);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
